package servlet;

import domain.Product;
import service.impl.ProdutcServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Author admin
 * @Date 2019/2/28 8:40
 */
@WebServlet(name = "EditServlet",urlPatterns = "/editServlet")
public class EditServlet extends HttpServlet {
    ProdutcServiceImpl service = new ProdutcServiceImpl();
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String pid = request.getParameter("pid");
        String image = request.getParameter("oldImage");
        String pname = request.getParameter("pname");
        String marketprice = request.getParameter("price");
        String shopprice = request.getParameter("disprice");
        String ishot = request.getParameter("ishot");
        String pdesc = request.getParameter("pdesc");
        Product p = new Product();
        p.setPid(Integer.parseInt(pid));
        p.setImage(image);
        p.setPname(pname);
        p.setMarketprice(Double.parseDouble(marketprice));
        p.setShopprice(Double.parseDouble(shopprice));
        p.setIshot(Integer.parseInt(ishot));
        p.setPdesc(pdesc);
        service.editProduct(p);
        response.sendRedirect("/findAll");
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String pid = request.getParameter("pid");

        List<Product> list = service.productAll(pid);
        //将数据放在request作用域中
        request.setAttribute("product",list);
        //请求转发到edit.jsp页面中
        request.getRequestDispatcher("/WEB-INF/edit.jsp").forward(request,response);
    }
}
